/**
 	InterfaceURL class
 */
package nrb.utils
{
	import flash.display.Sprite;
	import flash.events.*;
	import flash.net.*;
	import mx.controls.Alert;
	
	/**
	 	Type: Class
	 	@Params:none
	 	@return:none
	 	Desc: Class for communicating flex to server side using URLLoader AND URLRequest
	 */
    public class InterfaceURL extends Sprite {
		
		/**
		 	variables for loader, request, variables, listener
		 */
		private var loader:URLLoader;
		private var request:URLRequest;
		private var variables:URLVariables;
		private var listener:Function;
		
		/**
		 	Type: Controller
		 	@Params:none
		 	@return:none
		 	Desc: Initializes variables and configure listeners for loader
		 */
        public function InterfaceURL() {
            loader = new URLLoader();
            request = new URLRequest();
            variables = new URLVariables();
            configureListeners(loader);
        }
        
        /**
		 	Type: Public Function
		 	@Params:none
		 	@return:void
		 	Desc: processes request
		 */
        public function process(): void {
        	loader.load(request);
        }
        
        /**
		 	Type: Public Function
		 	@Params:value['URL'] AS String
		 	@return:void
		 	Desc: sets url
		 */
        public function set_url(value:String): void {
        	request = new URLRequest(value);
        }
        
        /**
		 	Type: Public Function
		 	@Params:value['POST' | 'GET'] AS String
		 	@return:void
		 	Desc: sets method type
		 */
        public function set_method(value:String): void {
        	if(value == "POST")
        		request.method = URLRequestMethod.POST;
        	else
        		request.method = URLRequestMethod.GET;
        }
        
        /**
		 	Type: Public Function
		 	@Params:obj[{var1:'String', var2:'String', var3:'String'}] AS Object
		 	@return:void
		 	Desc: sets parameters ready for sending to server side
		 */
        public function set_parameters(obj:Object): void {
        	for(var i:String in obj){
				variables[i] = obj[i];
			}
			//variables.test = 'jed';
			request.data = variables;
        }
        
        /**
		 	Type: Public Function
		 	@Params:listener[Function] AS Function
		 	@return:void
		 	Desc: sets listener
		 */
        public function set_listener(listener:Function): void {
        	this.listener = new Function();
        	this.listener = listener;
        }
        
		/**
		 	Type: Public Function
		 	@Params:dispatcher[IEventDispatcher] AS IEventDispatcher
		 	@return:void
		 	Desc: adds listeners to loader
		 */
        private function configureListeners(dispatcher:IEventDispatcher):void {
            dispatcher.addEventListener(Event.COMPLETE, completeHandler);
            //dispatcher.addEventListener(Event.OPEN, openHandler);
            //dispatcher.addEventListener(ProgressEvent.PROGRESS, progressHandler);
            dispatcher.addEventListener(SecurityErrorEvent.SECURITY_ERROR, securityErrorHandler);
            //dispatcher.addEventListener(HTTPStatusEvent.HTTP_STATUS, httpStatusHandler);
            dispatcher.addEventListener(IOErrorEvent.IO_ERROR, ioErrorHandler);
        }
	
		/**
		 	Type: Private Function
		 	@Params:event[Event] AS Event
		 	@return:void
		 	Desc: listener for successful transaction to server side
		 */
        private function completeHandler(event:Event):void {
            var loader:URLLoader = URLLoader(event.target);
            
            try
			{
			   // some code that could throw an error
			   var xml:XML = new XML(loader.data);
	            if(listener != null)
	            	listener(xml);
	            else
	            	Alert.show(xml.toXMLString());
			}
			catch (err:Error)
			{
			   // code to react to the error
			   listener();
			}
			finally
			{
			   // Code that runs whether or not an error was thrown. This code can clean
			    // up after the error, or take steps to keep the application running.
			    //listener();
			}
           
            	
        }

		/**
		 	Type: Private Function
		 	@Params:event[Event] AS Event
		 	@return:void
		 	Desc: listener for open
		 */
        private function openHandler(event:Event):void {
            Alert.show("openHandler: " + event);
        }
	
		/**
		 	Type: Private Function
		 	@Params:event[Event] AS Event
		 	@return:void
		 	Desc: listener for progress
		 */
        private function progressHandler(event:ProgressEvent):void {
            Alert.show("progressHandler loaded:" + event.bytesLoaded + " total: " + event.bytesTotal);
        }
		
		/**
		 	Type: Private Function
		 	@Params:event[Event] AS Event
		 	@return:void
		 	Desc: listener for security error
		 */
        private function securityErrorHandler(event:SecurityErrorEvent):void {
            Alert.show("securityErrorHandler: " + event);
        }
		
		/**
		 	Type: Private Function
		 	@Params:event[Event] AS Event
		 	@return:void
		 	Desc: listener for http status
		 */
        private function httpStatusHandler(event:HTTPStatusEvent):void {
            Alert.show("httpStatusHandler: " + event);
        }
		
		/**
		 	Type: Private Function
		 	@Params:event[Event] AS Event
		 	@return:void
		 	Desc: listener for error 
		 */
        private function ioErrorHandler(event:IOErrorEvent):void {
            Alert.show("ioErrorHandler: " + event);
        }

		
		
	}
}